package com.waimai.db.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.waimai.db.dao.BaseDao;
import com.waimai.db.dao.RestaurantDao;
import com.waimai.db.entity.Comment;
import com.waimai.db.entity.Food;
import com.waimai.db.entity.Order;
import com.waimai.db.entity.ReceiveAddress;
import com.waimai.db.entity.Restaurant;
import com.waimai.db.entity.User;

@Repository
@SuppressWarnings("unchecked")
public class RestaurantDaoImpl extends BaseDao implements RestaurantDao {

	public List<Restaurant> findBykeyWords(String keyWords) {
		keyWords = "%"+keyWords+"%";
		if(keyWords.contains("\"")){
			keyWords = keyWords.replace("\"", "");
		}
		String hql = "FROM Restaurant r WHERE r.restaurantName like :keyWords ORDER BY r.monthSales DESC, r.rest DESC";
		return getSession().createQuery(hql)
				.setParameter("keyWords", keyWords)
				.list();
	}

	public List<Food> getFoods(Integer restaurantId) {
		String hql = "FROM Food f WHERE f.category.categoryId IN (FROM Category c WHERE c.restaurant.restaurantId=:restaurantId)";
		List list = getSession().createQuery(hql)
				.setParameter("restaurantId", restaurantId).list();
		return list;
	}

	public Restaurant findById(Integer restaurantId) {
		return (Restaurant) getSession().get(Restaurant.class, restaurantId);
	}

	public List<Restaurant> getRestaurants() {
		// TODO Auto-generated method stub
		String hql = "FROM Restaurant r";
		return getSession().createQuery(hql).list();
	}

	public void save(Restaurant restaurant) {
		// TODO Auto-generated method stub
		getSession().save(restaurant);
	}

	public void update(Restaurant restaurant) {
		// TODO Auto-generated method stub
		getSession().update(restaurant);
	}

	public Order addOrder(Order order) {
		getSession().save(order);
		return order;
	}

	public ReceiveAddress addAddress(ReceiveAddress receiveaddress) {
		getSession().save(receiveaddress);
		return receiveaddress;
	}

	public List<Comment> getComments(Integer restaurantId, float minStarLevel,
			float maxStarLevel) {
		String hql = "FROM Comment c WHERE c.starLevel>=:minStarLevel AND c.starLevel<:maxStarLevel AND c.commentId IN "
				+ "(SELECT comment.commentId FROM Order o WHERE o.restaurant.restaurantId=:restaurantId)";
		List<Comment> comments = getSession().createQuery(hql)
				.setParameter("minStarLevel", minStarLevel)
				.setParameter("maxStarLevel", maxStarLevel)
				.setParameter("restaurantId", restaurantId).list();
		for (Comment comment : comments) {
			hql = "FROM User u WHERE u.userId=(SELECT user.userId FROM Order o WHERE o.comment.commentId=:commentId)";
			User user = (User) getSession().createQuery(hql)
					.setParameter("commentId", comment.getCommentId())
					.uniqueResult();
			comment.setUser(user);
		}
		return comments;
	}

	public List<Restaurant> getRestaurantsByAdminId(int adminId) {
		// TODO Auto-generated method stub
		String hql = "FROM Restaurant r where r.admin.adminId=?";
		List<Restaurant> restaurants = getSession().createQuery(hql)
				.setParameter(0, adminId).list();
		return restaurants;
	}

	public Restaurant findByRestuarentName(String restaurantName) {
		// TODO Auto-generated method stub
		String hql = "FROM Restaurant r where r.restaurantName=?";
		Restaurant restaurant = (Restaurant) getSession().createQuery(hql)
				.setParameter(0, restaurantName).uniqueResult();
		return restaurant;
	}

	public List<Comment> getCommentByAdminId(int adminId) {
		// TODO Auto-generated method stub
		String hql = "select r.restaurantName,r.address,o.orderTime,o.remark,c.starLevel,c.content FROM Order o,Restaurant r,Comment c where o.restaurant.restaurantId=r.restaurantId and o.comment.commentId=c.commentId and adminId=?";
		return getSession().createQuery(hql).setParameter(0, adminId).list();
	}

	public Restaurant getRestaurantsByName(String restaurantName) {
		// TODO Auto-generated method stub
		String hql="FROM Restaurant r where r.restaurantName=?";
		return (Restaurant) getSession().createQuery(hql).setParameter(0, restaurantName).uniqueResult();
	}
}
